package cn.qicaiwang.open.admin.controller.enterprise;

import cn.qicaiwang.open.app.entity.Enterprise;
import cn.qicaiwang.open.app.server.EnterpriseService;
import cn.qicaiwang.open.common.response.ResponseResult;
import cn.qicaiwang.open.common.utils.ProvinceContext;
import cn.qicaiwang.open.system.service.ISystemUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/enterprise")
public class AdminEnterpriseController {
    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 根据省份分页查询公司信息
     *
     * @param page
     * @param
     * @return
     */
    @PostMapping(value = "/page")
    public ResponseResult operateLoggerPage(@RequestBody Enterprise enterprise,Page page, @RequestParam String province) {
        Page<Enterprise> pageList = enterpriseService.pageList(page, province,enterprise);
        return ResponseResult.ok(pageList);

    }


    /**
     * 根据省份和条件查询公司信息
     * @param
     * @param province 省份名称(中文)
     * @return 查询结果
     */
    @GetMapping("/enterpriseInfo")
    public ResponseResult fuzzySearch( @RequestBody Enterprise enterprise, @RequestParam String province) {
        List<Map<String, Object>> result = enterpriseService.enterpriseInfo(enterprise, province);
        return ResponseResult.ok(result);
    }
    @PostMapping(value = "/add")
    public ResponseResult<?> add(
            @RequestParam String province,
            @RequestBody Enterprise enterprise) {

        Boolean b=  enterpriseService.save(enterprise);
        return ResponseResult.ok(b);
    }

    /**
     * 修改企业信息
     */
    @PostMapping("/update")
    public ResponseResult<Boolean> update(
            @RequestParam String province,
            @RequestBody Enterprise enterprise) {
        try {
            ProvinceContext.setProvince(province);
            if (enterprise.getId() == null) {
                return ResponseResult.error("企业ID不能为空");
            }
            boolean result = enterpriseService.update(enterprise);
            return ResponseResult.ok(result);
        } finally {
            ProvinceContext.clear();
        }
    }

    /**
     * 删除企业信息
     */
    @PostMapping("/delete/{id}")
    public ResponseResult<Boolean> delete(
            @RequestParam String province,
            @PathVariable Long id) {
        try {
            ProvinceContext.setProvince(province);
            boolean result = enterpriseService.removeById(id);
            return ResponseResult.ok(result);
        } finally {
            ProvinceContext.clear();
        }
    }
}






